var fansDao = require('../dao/FansDao');
var wechatRest = require('../../utils/wechatRest');
var Promise = require('promise');
var logger = require("log4js").getLogger('fansService.js');

var FansService = {};

//网页授权-用户保存或更新
FansService.snsFansSaveOrUpdate = function(openId, accountId, accessToken) {
	logger.info('wechat auth | snsFansSaveOrUpdate | openid'+openId);
  return new Promise(function(fulfill, reject){
	  Promise.all([
	    fansDao.getByOpenId(openId),
	    wechatRest.getSnsFans(openId, accessToken)]).done(function(res){
	    	var fans = res[0];
	    	var snsFans = res[1];
	    	if(fans&&fans.id){//更新用户
	    		snsFans.id = fans.id;
	    		fansDao.update(snsFans).then(function(rows){
	    			fulfill(snsFans);
	    		});
	    	}else{//保存新用户
	    		snsFans.accountId = accountId;
	    		fansDao.save(snsFans).then(function(rows){
					snsFans.id = rows.insertId;
	    			fulfill(snsFans);
	    		});
	    	}
	    },function(err){
	    	reject(err);
	    });

  });
};



//网页授权-根据openid获取用户
FansService.snsFansGetByOpenId = function(openId){
	return new Promise(function(fulfill, reject){
		fansDao.getByOpenId(openId).done(function(row){
			if(row.openId){
				fulfill(row);
			}else{
				fulfill({openId : openId});
			}
		},function(err){
			if(err) reject(err);
		})
	});
};

FansService.getByOpenId = function(openId){
	return fansDao.getByOpenId(openId);
};

// 保存或更新粉丝信息
FansService.saveOrUpdate = function(openId, account, callback) {
  return new Promise(function(fulfill, reject){
	  Promise.all([
	    fansDao.getByOpenId(openId),
	    wechatRest.getSnsFans(openId, accessToken)]).then(function(res){
		  logger.info(res);
	    	var fans = res[0];
	    	var snsFans = res[1];
	    	if(fans&&fans.id){//更新用户
	    		snsFans.id = fans.id;
	    		fansDao.update(snsFans).then(function(rows){
	    			fulfill(snsFans);
	    		});
	    	}else{//保存新用户
	    		snsFans.accountId = accountId;
	    		fansDao.save(snsFans).then(function(rows){
	    			fulfill(snsFans);
	    		});
	    	}
	    },function(err){
	    	reject(err);
	    });
  });
};



// 更新粉丝关注信息
FansService.updateSubscribe = function(subscribe, openId, callback) {
  fansDao.getByOpenId(openId, function(fans) {
    if (fans == undefined) {
      callback('query fans error, openId : ' + openId);
    } else {
      fansDao.updateSubscribe(subscribe, fans.id, function(rows) {
        if (rows) {
          callback(null);
        } else {
          callback('update fans subscribe error, fansId : ' + id);
        }
      });
    }
  });
};


FansService.tryGetByOpenId = function(openId, account,callback){
  fansDao.getByOpenId(openId, function(fans){
    if(fans){
    	callback(null, fans);
    }else{       
        wechatRest.getFans(openId, account.accessToken, function(results) {
    		if(results&&results.subscribe&&results.subscribe==1){  
    	        results.accountId = account.id;
    	        results.createTime = new Date();
    	        results.authorize = 1;
    	        fansDao.save(results, function(r) {
    	        	logger.info("103,get fans from rest and save fans "+results.nickname);
    	        });
    	        callback(null,results);
    	      }else{
    	    	callback(null,{openId:openId,nickname:'',authorize:0});
    		}
        });//wechatRest.getFans    
    }    
  });//fansDao.getByOpenId
};

module.exports = FansService;